<?php

namespace backend\controllers;

use Yii;
use common\models\Achievement;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * AchievementController implements the CRUD actions for Achievement model.
 */
class AchievementController extends Controller
{
    /**
     * @inheritdoc
     */
//    public function behaviors()
//    {
//        return [
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'delete' => ['POST'],
//                ],
//            ],
//        ];
//    }

    /**
     * Lists all Achievement models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model = new Achievement;
        $condition = '';
        if (Yii::$app->request->isPost) {
            $model->load(Yii::$app->request->post());
//            print_r($model->attributes);die;
            if(empty($model->full_name) && empty($model->subject) && empty($model->mobile) && empty($model->id_card)){
                \Yii::$app->getSession()->setFlash('danger', "请至少输入一个查询条件！");
                return $this->redirect(Yii::$app->request->getReferrer());
            }
            $condition .= empty($model->full_name) ? "" : (empty($condition) ? "full_name like '%" . $model->full_name . "%'" : " and full_name like '%" . $model->full_name . "%'");
            $condition .= empty($model->subject) ? "" : (empty($condition) ? "subject like '%" . $model->subject . "%'" : " and subject like '%" . $model->subject . "%'");
            $condition .= empty($model->mobile) ? "" : (empty($condition) ? "mobile like '%" . $model->mobile . "%'" : " and mobile like '%" . $model->mobile . "%'");
            $condition .= empty($model->id_card) ? "" : (empty($condition) ? "id_card like '%" . $model->id_card . "%'" : " and id_card like '%" . $model->id_card . "%'");
            $dataProvider = new ActiveDataProvider([
                'query' => Achievement::find()->where($condition),
            ]);
        }else{
            $dataProvider = new ActiveDataProvider([
                'query' => Achievement::find()->where(['id'=>'999999999']),
            ]);
        }
        $dataProvider->setSort(false);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'model' => $model,
        ]);
    }

    /**
     * Displays a single Achievement model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Achievement model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Achievement();

        if (Yii::$app->request->isPost) {
            $model->load(Yii::$app->request->post());
            if(empty($model->full_name) || empty($model->subject) || empty($model->mobile) || empty($model->id_card) || empty($model->pass) || empty($model->score)){
                \Yii::$app->getSession()->setFlash('danger', "姓名，科目，电话，身份证，分数，合格不能为空！");
                return $this->redirect(Yii::$app->request->getReferrer());
            }
            $model->created = date('Y-m-d H:i:s');
            $model->updated = date('Y-m-d H:i:s');
            if ($model->save()) {
                \Yii::$app->getSession()->setFlash('success', "资料添加成功！");
                return $this->redirect(['create']);
            }
        }
        $dataProvider = new ActiveDataProvider([
            'query' => Achievement::find(),
        ]);
        $dataProvider->setSort(false);
        return $this->render('create', [
            'model' => $model,
            'dataProvider' => $dataProvider,
        ]);

    }

    /**
     * Updates an existing Achievement model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            \Yii::$app->getSession()->setFlash('success', "资料修改成功！");
            return $this->redirect(['index']);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Achievement model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        \Yii::$app->getSession()->setFlash('success', "资料删除成功！");
        return $this->redirect(['index']);
    }

    public function actionDeleteall()
    {
        $achievement= new Achievement();
        $id = Yii::$app->request->post();
        if(!empty($id['id'])){
            $condition = 'id in ('.implode(',',$id['id']).')';
            $achievement->deleteAll($condition);//Words换成你的模型
        }
        echo json_encode("ok");
    }

    /**
     * Finds the Achievement model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Achievement the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Achievement::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
